package xyz.wanwanwan.www.hrm.web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xyz.wanwanwan.www.hrm.domain.CoursePromotion;
import xyz.wanwanwan.www.hrm.service.ISavePromotionToRedis;
import xyz.wanwanwan.www.hrm.util.AjaxResult;
import xyz.wanwanwan.www.hrm.util.RedisUtils;

import java.util.List;

@RestController
@RequestMapping("/redis")
public class CacheController {
    @Autowired
    private ISavePromotionToRedis savePromotionToRedis;
    /**
     * 保存到redis
     * @RequestParam key  /redis/set?key=usernamae&value=hshn
     * @RequestParam value
     * @return
     */
    @PostMapping("/set")
    public AjaxResult setToRedis(@RequestParam("key") String key, @RequestParam("value") String value){
        try {
            RedisUtils.INSTANCE.set(key,value);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存失败");
        }
    }
    @PostMapping("/save")
    public AjaxResult savePromotion(@RequestBody CoursePromotion coursePromotion){
        try {
            savePromotionToRedis.savePromotion(coursePromotion);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("存储促销失败");
        }
    }
    @PostMapping("/getListCoursePromotion")
    public AjaxResult getListCoursePromotion(){
        try {
            List<CoursePromotion> result = savePromotionToRedis.getListCoursePromotion();
            return AjaxResult.me().setResultObj(result);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("存储促销失败");
        }
    }
    @PostMapping("/getBuyCoursePromotion/{id}")
    public byte[] getBuyCoursePromotion(@RequestParam("id")Long id){
        try {
            byte[] buyCoursePromotion = savePromotionToRedis.getBuyCoursePromotion(id);
            return buyCoursePromotion;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    @PostMapping("/setex")
    public AjaxResult setImageCodeToRedis(@RequestParam("key") String key,
                                          @RequestParam("seconds") int seconds,
                                          @RequestParam("value") String value){
        try {
            RedisUtils.INSTANCE.setex(key,seconds,value);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存失败");
        }
    }

    /**
     * 从redis中获取
     * @PathVariable key /redis/get/username
     * @return
     */
    @GetMapping("/get/{key}")
    public AjaxResult getFromRedis(@PathVariable("key") String key){
        try {
            String result = RedisUtils.INSTANCE.get(key);
            return AjaxResult.me().setResultObj(result);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取失败");
        }
    }

    /**
     * 删除缓存
     * @param key
     * @return
     */
    @GetMapping("/del/{key}")
    public AjaxResult delRedis (@PathVariable("key") String key){
        try {
            Long del = RedisUtils.INSTANCE.del(key);
            return AjaxResult.me().setResultObj(del);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败");
        }
    }
}
